package org.yanfengying.org.controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.yanfengying.org.service.IFsatdflService;
import org.yanfengying.utils.AjaxResult;
import org.yanfengying.utils.FastdfsUtil;

@RestController
@RequestMapping("/fastdfl")
public class FastdflController {
    @Autowired
    private IFsatdflService fsatdflService;
    @PostMapping
    public AjaxResult goLogo(MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String extname = FilenameUtils.getExtension(originalFilename);
            byte[] bytes = file.getBytes();
            String path = fsatdflService.getPath(bytes, extname);
            System.out.println(path);
            return AjaxResult.me().setResult(path);
        } catch (Exception e) {
            e.printStackTrace();
        return AjaxResult.me().setMessage("上传失败"+e.getMessage()).setSuccess(false);
        }
    }
    @GetMapping
    public AjaxResult delLgo(String path){
        try {
            fsatdflService.delLogo(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me();
    }

}
